package com.jeeplus.test.jlnu.student.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AdmissionInfo;
import com.jeeplus.test.jlnu.admissionInfo.mapper.AdmissionInfoMapper;
import com.jeeplus.test.jlnu.student.domain.entity.BkStudentInfo;
import com.jeeplus.test.jlnu.student.domain.result.NameAndPassword;
import com.jeeplus.test.jlnu.student.mapper.BkStudentInfoMapper;
import com.jeeplus.test.jlnu.student.service.BkStudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BkStudentInfoServiceImpl extends ServiceImpl<BkStudentInfoMapper, BkStudentInfo> implements BkStudentInfoService {

    @Autowired
    private BkStudentInfoMapper bkStudentInfoMapper;
    @Autowired
    private AdmissionInfoMapper admissionInfoMapper;

    @Override
    public NameAndPassword check(String studentId, String idCard) {
        if (idCard != null && idCard.length() == 6) {
            BkStudentInfo bkStudentInfo = new LambdaQueryChainWrapper<>(bkStudentInfoMapper)
                    .eq(BkStudentInfo::getStudentId, studentId)
                    .like(BkStudentInfo::getIdCardNumber, "%" + idCard)
                    .one();
            if (bkStudentInfo == null) {
                throw new RuntimeException("学生信息有误");
            }
            return new NameAndPassword().setStudentId(studentId).setIdCard(bkStudentInfo.getIdCardNumber());
        } else {
            throw new RuntimeException("身份证号码格式错误");
        }
    }

    @Override
    public BkStudentInfo getBkStudentInfo(String studentId, String idCard) {
        LambdaQueryChainWrapper<BkStudentInfo> wrapper = new LambdaQueryChainWrapper<>(bkStudentInfoMapper);
         wrapper.eq(BkStudentInfo::getStudentId, studentId)
                .eq(BkStudentInfo::getIdCardNumber, idCard);
        return wrapper.one();
    }

    @Override
    public Boolean bkStudentInfoUpdate(BkStudentInfo bkStudentInfo) {
        LambdaUpdateChainWrapper<BkStudentInfo> wrapper = new LambdaUpdateChainWrapper<>(bkStudentInfoMapper);
        String id = bkStudentInfo.getStudentId();
        if (ObjectUtil.isNotEmpty(bkStudentInfo.getAge())){
            wrapper.set(BkStudentInfo::getAge,bkStudentInfo.getAge());
            admissionInfoMapper.updateAge(bkStudentInfo.getAge(),id);
        }
        if (ObjectUtil.isNotEmpty(bkStudentInfo.getPoliticalStatus())){
            wrapper.set(BkStudentInfo::getPoliticalStatus,bkStudentInfo.getPoliticalStatus());
            admissionInfoMapper.updatePoliticalOutlook(bkStudentInfo.getPoliticalStatus(),id);
        }
        if (ObjectUtil.isNotEmpty(bkStudentInfo.getContactNumber())){
            wrapper.set(BkStudentInfo::getContactNumber,bkStudentInfo.getContactNumber());
            admissionInfoMapper.updatePhoneNum(bkStudentInfo.getContactNumber(),id);
        }
        if (ObjectUtil.isNotEmpty(bkStudentInfo.getInterests())){
            wrapper.set(BkStudentInfo::getInterests,bkStudentInfo.getInterests());
        }
        if (ObjectUtil.isNotEmpty(bkStudentInfo.getSpecialties())){
            wrapper.set(BkStudentInfo::getSpecialties,bkStudentInfo.getSpecialties());
        }
        if (ObjectUtil.isNotEmpty(bkStudentInfo.getRecipient())){
            wrapper.set(BkStudentInfo::getRecipient,bkStudentInfo.getRecipient());
        }
        if (ObjectUtil.isNotEmpty(bkStudentInfo.getFamilyAddress())){
            wrapper.set(BkStudentInfo::getFamilyAddress,bkStudentInfo.getFamilyAddress());
            admissionInfoMapper.updateHomeAddress(bkStudentInfo.getFamilyAddress(),id);
        }
        if (ObjectUtil.isNotEmpty(bkStudentInfo.getPostalCode())){
            wrapper.set(BkStudentInfo::getPostalCode,bkStudentInfo.getPostalCode());
            admissionInfoMapper.updatePostalCode(bkStudentInfo.getPostalCode(),id);
        }
        if (ObjectUtil.isNotEmpty(bkStudentInfo.getFamilyEconomicStatus())){
            wrapper.set(BkStudentInfo::getFamilyEconomicStatus,bkStudentInfo.getFamilyEconomicStatus());
        }
        //根据id修改
        wrapper.eq(BkStudentInfo::getId, bkStudentInfo.getId());
        return wrapper.update();
    }
}
